﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for SearchCustControl.xaml
	/// </summary>
	public partial class SearchCustControl : UserControl
	{
		public SearchCustControl()
		{
			this.InitializeComponent();
		}

		private void searchCustClick(object sender, System.Windows.RoutedEventArgs e)
		{
			rgrid.IsReadOnly = true;//none of the database values should be directly editable from the search panel
            rgrid.AutoGenerateColumns = false;
			Database dbase = Database.Instance;
				if (radioName.IsChecked == true)//  search by name
				{
					//standard LINQ-style search. Read db4o tutorial segment on searching for more information
					dbase.result = from Invoice I in dbase.db
												   where I.defCust.name.Contains(tsbox.Text.ToUpper())
												   select I;

                    rgrid.ItemsSource = dbase.result;
				}
				else if (radioAddress.IsChecked == true)// search by address
				{
					dbase.result = from Invoice I in dbase.db
												   where I.defCust.bAddress.Contains(tsbox.Text.ToUpper()) || 
															I.jAddress.Contains(tsbox.Text.ToUpper())
												   select I;
                    rgrid.ItemsSource = dbase.result;
				}
		}

		private void startNewInvoice(object sender, System.Windows.RoutedEventArgs e)
		{
			if (MessageBox.Show("Start a new invoice for " + ((Invoice)rgrid.SelectedItem).defCust.name + "?","Invoice", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
			{
				Invoice profile = new Invoice();
            	if (((Invoice)rgrid.SelectedItem).invoiceStatus == 0)
            	{
                	var prof = new CustProfile((Invoice)rgrid.SelectedItem);
                	prof.Show();
            	}
            	else
            	{
					Database dbase = Database.Instance;
                	foreach (Invoice inv in dbase.result)
                	{
                    	if (inv.inv_num == ((Invoice)rgrid.SelectedItem).inv_num)
                    	{
                        	profile.defCust = inv.defCust;
                    	}
                	}
                	var custProfile = new CustProfile(profile);
                	custProfile.Show();
            	}
        	}
		}

		private void viewCurInv(object sender, System.Windows.RoutedEventArgs e)
		{
            var invProfile = new InvoiceProfile((Invoice)rgrid.SelectedItem);
			invProfile.Show();
		}

		private void onMouseClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			Invoice profile = null;
			Database dbase = Database.Instance;
            foreach (Invoice inv in dbase.result)
            {
                if (inv.inv_num == ((Invoice)rgrid.SelectedItem).inv_num)
                {
                    profile = inv;
                }
            }
			
            
			var custProfile = new CustProfile(profile);
			custProfile.Show();
		}

		// determines visibility of start estimate, new invoice and current invoice buttons from grid
		private void setInvButtonVis(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
		{
			Database dbase = Database.Instance;
			foreach (Invoice inv in dbase.result)
            {
                if (rgrid.SelectedItem != null)
                    newInv.Visibility = Visibility.Visible;

                if (((Invoice)rgrid.SelectedItem).invoiceStatus > 0)
				{
					newInv.Visibility = Visibility.Visible;
					startEst.Visibility = Visibility.Collapsed;
                    curInv.Visibility = Visibility.Visible;
					
				}	
                else if (((Invoice)rgrid.SelectedItem).invoiceStatus == 0)
                {
                    newInv.Visibility = Visibility.Collapsed;
					startEst.Visibility = Visibility.Visible;
                    curInv.Visibility = Visibility.Collapsed;
                }
                

            }
		}

		private void startNewEst(object sender, System.Windows.RoutedEventArgs e)
		{
			    if (MessageBox.Show("Start an estimate for " + ((Invoice)rgrid.SelectedItem).defCust.name + "?","Invoice", MessageBoxButton.YesNo) == MessageBoxResult.Yes){
				((Invoice)rgrid.SelectedItem).invoiceStatus = 1;   // set invoice status to Estimate
				((Invoice)rgrid.SelectedItem).estimateDate = DateTime.Now.Date; // set estimate date

				Database dbase = Database.Instance;
				dbase.db.Store((Invoice)rgrid.SelectedItem);         
				dbase.db.Commit();
		
        		InvoiceProfile newInv = new InvoiceProfile((Invoice)rgrid.SelectedItem);
				newInv.Show();
			}
		}
		
		public void populateGrid()
        {
            DataGridTextColumn invoiceCol = new DataGridTextColumn();
            invoiceCol.Header = "Invoice Number";
            invoiceCol.Binding = new Binding("inv_num");
			invoiceCol.Binding.StringFormat = ("#{0}");
            rgrid.Columns.Add(invoiceCol);
			
			DataGridTextColumn invoiceStat = new DataGridTextColumn();
            invoiceStat.Header = "P.O.";
            invoiceStat.Binding = new Binding("invoiceStat");
            rgrid.Columns.Add(invoiceStat);

            DataGridTextColumn nameCol = new DataGridTextColumn();
            nameCol.Header = "Client Name";
            nameCol.Binding = new Binding("defCust.name");
            rgrid.Columns.Add(nameCol);

			DataGridTextColumn bAddrCol = new DataGridTextColumn();
            bAddrCol.Header = "Billing Address";
            bAddrCol.Binding = new Binding("defCust.bAddress");
            rgrid.Columns.Add(bAddrCol);
			
            DataGridTextColumn jAddrCol = new DataGridTextColumn();
            jAddrCol.Header = "Service Address";
            jAddrCol.Binding = new Binding("jAddress");
            rgrid.Columns.Add(jAddrCol);

            DataGridTextColumn hPhoneCol = new DataGridTextColumn();
            hPhoneCol.Header = "Home Phone";
            hPhoneCol.Binding = new Binding("defCust.hPhone");
            rgrid.Columns.Add(hPhoneCol);
			
			DataGridTextColumn cPhoneCol = new DataGridTextColumn();
            cPhoneCol.Header = "Cell Phone";
            cPhoneCol.Binding = new Binding("defCust.cPhone");
            rgrid.Columns.Add(cPhoneCol);

            DataGridTextColumn emailCol = new DataGridTextColumn();
            emailCol.Header = "Email";
            emailCol.Binding = new Binding("defCust.email");
            rgrid.Columns.Add(emailCol);
           
			DataGridTextColumn notesCol = new DataGridTextColumn();
            notesCol.Header = "Notes";
            notesCol.Binding = new Binding("notes");
            rgrid.Columns.Add(notesCol);
        }

	}
}